package com.cskaoyan.service.admin;

import com.cskaoyan.bean.MarketCategoryExample;
import com.cskaoyan.bean.MarketCategory;
import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.bo.admin.category.MarketCategoryUpdateBo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.admin.category.MarketCategoryCreateVo;
import com.cskaoyan.bean.vo.admin.category.MarketCategoryL1Vo;
import com.cskaoyan.bean.vo.admin.category.MarketCategoryListVo;
import com.cskaoyan.bean.vo.wx.CategoryVO;
import com.cskaoyan.bean.vo.wx.ListCategoryVO;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.utils.DateUtil;
import com.cskaoyan.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * 商场管理/品牌制造商模块的service层实现
 *
 * @Author: zhang
 * @Date: 2022/11/20 15:28
 * @Since: jdk1.8
 */
@Service
public class MarketCategoryServiceImpl implements MarketCategoryService {
    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public CommonData<MarketCategoryListVo> list() {

        List<MarketCategoryListVo> categories = marketCategoryMapper.selectAllCategories();
        PageHelper.startPage(1, categories.size());
        PageInfo<MarketCategoryListVo> pageInfo = new PageInfo<>(categories);
        return CommonData.data(pageInfo);
    }

    @Override
    public CommonData<MarketCategoryL1Vo> l1() {
        List<MarketCategoryL1Vo> categories = marketCategoryMapper.selectAllCategoriesWithLevelL1();
        PageHelper.startPage(1, categories.size());
        PageInfo<MarketCategoryL1Vo> pageInfo = new PageInfo<>(categories);
        return CommonData.data(pageInfo);
    }

    @Override
    public CategoryVO selectCategoryDetail(Integer id) {
        CategoryVO categoryVO = new CategoryVO();

        MarketCategory marketCategory = marketCategoryMapper.selectByPrimaryKey(id);
        int pid = marketCategory.getPid();
        if(pid != 0){  //二级类目
            categoryVO.setCurrentCategory(marketCategory); // 二级类目,当前类目是自己
            MarketCategory marketCategoryParent = marketCategoryMapper.selectParent(pid);
            categoryVO.setParentCategory(marketCategoryParent);  //设置好父
            List<MarketCategory> marketCategories = marketCategoryMapper.selectBrothers(pid);
            categoryVO.setBrotherCategory(marketCategories); //设置好兄弟
        }else { // 一级类目
            List<MarketCategory> marketCategories = marketCategoryMapper.selectBrothers(id); //找出所有儿子
            if(null != marketCategories){
                categoryVO.setCurrentCategory(marketCategories.get(0));  //儿子中的第一个
                categoryVO.setBrotherCategory(marketCategories);
                categoryVO.setParentCategory(marketCategory);
            }
        }
        return categoryVO;
    }

    @Override
    public ListCategoryVO getListCategoryVO(String categoryId, int page, int limit) {
        ListCategoryVO listCategoryVO = new ListCategoryVO();

        List <MarketGoods> list = marketGoodsMapper.selectGoodsByCatId(Integer.parseInt(categoryId),limit);
        listCategoryVO.setList(list);

        MarketCategory marketCategory = marketCategoryMapper.selectByPrimaryKey(Integer.valueOf(categoryId));
        int pid = marketCategory.getPid();

        List<MarketCategory> marketCategories = marketCategoryMapper.selectBrothers(pid);
        listCategoryVO.setFilterCategoryList(marketCategories);  //兄弟设置好
        listCategoryVO.setPage(1);
        listCategoryVO.setLimit(limit);

        int total = 0;
        for (MarketCategory category : marketCategories) {
            int id = category.getId();
            total += marketGoodsMapper.selectCountByCatId(id);
        }
        listCategoryVO.setTotal(total);

        int pages = 0;
        if( (total % limit) == 0){
            pages = total / limit;
        }else {
            pages = total / limit + 1;
        }
        listCategoryVO.setPages(pages);

        return listCategoryVO;
    }

    @Override
    public MarketCategoryCreateVo create(MarketCategory marketCategory) throws SQLException {
        // 判断商品类目是否已经创建了
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(marketCategory.getName());
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example);
        if (marketCategories.size() != 0) {
            return null;
        }
        // 没有创建再创建
        Date date = DateUtil.getDate();
        marketCategory.setAddTime(date);
        marketCategory.setUpdateTime(date);
        marketCategory.setSortOrder(new Byte(StringUtil.getRandomNum(2)));
        marketCategory.setDeleted(false);
        if (marketCategory.getPid() == 0 && "L2".equals(marketCategory.getLevel())) {
            marketCategory.setLevel("L1");
        }
        int affectedRows = marketCategoryMapper.insert(marketCategory);
        if (affectedRows == 0) {
            throw new SQLException("insert into market_brand failed");
        }
        return new MarketCategoryCreateVo(marketCategory.getId(), marketCategory.getName(), marketCategory.getKeywords(), marketCategory.getDesc(), marketCategory.getPid(), marketCategory.getIconUrl(), marketCategory.getPicUrl(), marketCategory.getLevel(), marketCategory.getAddTime(), marketCategory.getUpdateTime());
    }

    @Override
    public int update(MarketCategoryUpdateBo marketCategoryUpdateBo) {
        if (marketCategoryUpdateBo.getChildren() != null) {
            // 修改一级类目
            MarketCategory marketCategory = new MarketCategory();
            marketCategory.setId(marketCategoryUpdateBo.getId());
            marketCategory.setName(marketCategoryUpdateBo.getName());
            marketCategory.setKeywords(marketCategoryUpdateBo.getKeywords());
            marketCategory.setDesc(marketCategoryUpdateBo.getDesc());
            marketCategory.setPid(marketCategoryUpdateBo.getPid());
            marketCategory.setIconUrl(marketCategoryUpdateBo.getIconUrl());
            marketCategory.setPicUrl(marketCategoryUpdateBo.getPicUrl());
            marketCategory.setLevel(marketCategoryUpdateBo.getLevel());
            Date date = DateUtil.getDate();
            marketCategory.setUpdateTime(date);
            return marketCategoryMapper.updateByPrimaryKeySelective(marketCategory);
        }
        // 修改二级类目
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setId(marketCategoryUpdateBo.getId());
        marketCategory.setName(marketCategoryUpdateBo.getName());
        marketCategory.setKeywords(marketCategoryUpdateBo.getKeywords());
        marketCategory.setDesc(marketCategoryUpdateBo.getDesc());
        marketCategory.setPid(marketCategoryUpdateBo.getPid());
        marketCategory.setIconUrl(marketCategoryUpdateBo.getIconUrl());
        marketCategory.setPicUrl(marketCategoryUpdateBo.getPicUrl());
        marketCategory.setLevel(marketCategoryUpdateBo.getLevel());
        Date date = DateUtil.getDate();
        marketCategory.setUpdateTime(date);
        return marketCategoryMapper.updateByPrimaryKeySelective(marketCategory);
    }

    @Override
    public int delete(MarketCategoryUpdateBo marketCategoryUpdateBo) {
        Integer id = marketCategoryUpdateBo.getId();
        int affectedRows = 0;
        affectedRows = marketCategoryMapper.updateDeletedById(id);
        List<MarketCategoryUpdateBo.ChildrenBean> children = marketCategoryUpdateBo.getChildren();
        if (children != null) {
            for (MarketCategoryUpdateBo.ChildrenBean child : children) {
                affectedRows = marketCategoryMapper.updateDeletedById(child.getId());
            }
        }
        return affectedRows;
    }


    public List<MarketCategory> queryL2ByIds(List<Integer> ids) {
        MarketCategoryExample example = new MarketCategoryExample();
        example.or().andIdIn(ids).andLevelEqualTo("L2").andDeletedEqualTo(false);
        return marketCategoryMapper.selectByExample(example);
    }
}
